package org.dut.community.service;

import org.dut.community.dao.CommentMapper;
import org.dut.community.entity.Comment;
import org.dut.community.util.CommunityConstant;
import org.dut.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentService implements CommunityConstant {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private SensitiveFilter sensitiveFilter;
    @Autowired
    private LikeService likeService;

    /**
     * 查询评论
     * @param entityType 评论类型
     * @param entityId 评论实体id
     */
    public List<Comment> findCommentsByEntity(int entityType, int entityId, int offset, int limit, int orderMode) {
        if(orderMode != 2){
            return commentMapper.selectCommentsByEntity(entityType, entityId, offset, limit, orderMode);
        }else{
            List<Comment> comments = commentMapper.selectCommentsByEntity(entityType, entityId, 0, Integer.MAX_VALUE, 1);
            Map<Comment,Integer> commentMap = new HashMap<>();
            for(Comment comment : comments){
               int likeCount = (int) likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, comment.getId());
               commentMap.put(comment,likeCount);
            }
            // 按赞数对评论降序排序
            comments.sort((c1,c2) -> Integer.compare(commentMap.get(c2), commentMap.get(c1)));
            int toIndex = Math.min(offset+limit,comments.size());
            return comments.subList(offset,toIndex);
        }
    }

    /**
     * 查询评论数量
     * @param entityType 评论类型
     * @param entityId 评论实体id
     * @return
     */
    public int findCommentsCount(int entityType, int entityId) {
        return commentMapper.selectCommentsCount(entityType, entityId);
    }

    /**
     * 新增评论
     * @param comment
     * @return
     */
    public int addComment(Comment comment) {
        if(comment == null) {
            throw new IllegalArgumentException("参数不能为空！");
        }
        // 转义html标签，过滤敏感词
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
        comment.setContent(sensitiveFilter.filter(comment.getContent()));
        int rows = commentMapper.insertComment(comment);
        if(comment.getEntityType() == CommunityConstant.ENTITY_TYPE_POST) {
            // 刷新帖子评论数量
            discussPostService.updateCommentCount(comment.getEntityId(),
                    findCommentsCount(comment.getEntityType(), comment.getEntityId()));
        }
        return rows;
    }

    // 查询某个用户的所有评论
    public List<Comment> findUserComments(int userId, int offset, int limit) {
        return commentMapper.selectCommentByUser(userId, offset, limit);
    }

    // 查询某个用户的评论数量
    public int findUserCount(int userId) {
        return commentMapper.selectCommentCountByUser(userId);
    }

    // 根据id查询评论
    public Comment findCommentById(int id) {
        return commentMapper.selectCommentById(id);
    }
}
